package com.allwees.bs.c.module.user.mapper;

import com.google.common.collect.Lists;
import com.allwees.bs.c.module.user.dto.MessageDto;
import com.allwees.bs.c.module.user.entity.MessageEntity;
import com.allwees.bs.core.modelbase.page.PageParams;
import com.allwees.bs.core.modelbase.page.PaginateDTO;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class MessageMapper {


    public PaginateDTO<MessageDto> entity2Dto(Page<MessageEntity>  page, PageParams pageParams) {
    	List<MessageEntity> entities = page.getContent();
    	long total = page.getTotalElements();

    	PaginateDTO<MessageDto> pageResult = null;
        if (entities == null) {
        	pageResult = PaginateDTO.of(0, 0, pageParams);
        }
        List<MessageDto> list = Lists.newArrayList();
        entities.forEach(entity -> list.add(new MessageDto(entity)));
        pageResult = PaginateDTO.of(total, entities.size(), pageParams);
        pageResult.setResults(list);
        return pageResult;
    }

}
